package com.puman.beautyfarmcounselor.ui.setting;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.IndexActivity;
import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.http.AjaxCallBack;
import com.puman.beautyfarmcounselor.http.AjaxParams;
import com.puman.beautyfarmcounselor.ui.core.BaseFragment;
import com.puman.beautyfarmcounselor.ui.home.model.CustomerDetail;
import com.puman.beautyfarmcounselor.util.Config;
import com.puman.beautyfarmcounselor.util.FinalHttp;
import com.puman.beautyfarmcounselor.util.MD5;
import com.puman.beautyfarmcounselor.util.MethedUtil;
import com.puman.beautyfarmcounselor.util.PublicData;
import com.puman.beautyfarmcounselor.util.XTHttp;
import com.puman.beautyfarmcounselor.util.config.GlobalData;
import com.puman.beautyfarmcounselor.util.config.RequestAddr;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

/**
 * 设置反馈
 */
@EFragment(R.layout.setting_msg)
public class SettingMsgFragment extends BaseFragment {
    //   public static String url=Config.baseUrl;
    public static String append_url = "employee/employee/feedBack";


    @ViewById(R.id.msg_title)
    EditText editText_msg_title;
    @ViewById(R.id.msg_content)
    EditText editText_msg_content;
    @ViewById(R.id.msg_type)
    LinearLayout msg_linearLayout;
    @ViewById(R.id.msg_type_1)
    TextView msg_type1;
    @ViewById(R.id.msg_type_2)
    TextView msg_type2;
    @ViewById(R.id.msg_type_3)
    TextView msg_type3;

    @ViewById(R.id.base_title)
    TextView title;

    @ViewById(R.id.back_btn)
    ImageView backBtn;

    public int msg_type = 1;

    @AfterViews
    public void updateView() {
        title.setText("反馈");
        backBtn.setVisibility(View.VISIBLE);
    }

    @Click({R.id.msg_type_1, R.id.msg_type_2, R.id.msg_type_3, R.id.msg_cancle, R.id.msg_sure})
    public void Onclick(View view) {
        switch (view.getId()) {
            case R.id.msg_type_1:
                msg_type = 1;
                msg_linearLayout.setBackgroundResource(R.drawable.feedback_1);
                msg_type1.setTextColor(Color.WHITE);
                msg_type2.setTextColor(Color.parseColor("#949393"));
                msg_type3.setTextColor(Color.parseColor("#949393"));

                break;
            case R.id.msg_type_2:
                msg_type = 2;
                msg_linearLayout.setBackgroundResource(R.drawable.feedback_2);
                msg_type2.setTextColor(Color.WHITE);
                msg_type1.setTextColor(Color.parseColor("#949393"));
                msg_type3.setTextColor(Color.parseColor("#949393"));
                break;
            case R.id.msg_type_3:
                msg_linearLayout.setBackgroundResource(R.drawable.feedback_3);
                msg_type3.setTextColor(Color.WHITE);
                msg_type1.setTextColor(Color.parseColor("#949393"));
                msg_type2.setTextColor(Color.parseColor("#949393"));
                msg_type = 3;
                break;
            case R.id.msg_sure:
                save();
                break;
            case R.id.msg_cancle:
                cancle();
        }
    }


    XTHttp xtHttp = new XTHttp();
    String url = "";
    String param = "";

    //    content	反馈内容	string
//    emp_id	员工id	number
//    name	员工姓名	string
//    title	标题	string
//    type	反馈类型	number	0-建设意见、1-出错报错、2-求助帮助
    private void save() {
        param = "emp_id=" + GlobalData.counselor.getData().getEmployee_id() +
                "&content=" + editText_msg_content.getText().toString().trim() +
                "&name=" + GlobalData.counselor.getData().getName() +
                "&title=" + editText_msg_title.getText().toString().trim() +
                "&type=" + msg_type;
        url = RequestAddr.feedback + param;
        xtHttp.get(url, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(IndexActivity.getInstace(), "网络不稳定，请稍后再试", Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
//                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    PublicData publicData;

    private void dealData(Object o) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            publicData = mapper.readValue(o.toString(), PublicData.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (publicData == null)
            return;
        MethedUtil.UtilToast(getActivity(), publicData.getMsg());
        if (publicData.isFlag())
            MainActivity.getInstace().getSupportFragmentManager().popBackStack();

    }

    //    public void save(){
//        String timestamp = new Date().getTime() + "";
//        String sign = MD5.getMD5ofStr(com.puman.beautyfarmcounselor.util.config.Config.secret + "&"
//                + com.puman.beautyfarmcounselor.util.config.Config.public_key + "&" + timestamp);
//        try {
//            AjaxParams params = new AjaxParams();
//            params.put("content", editText_msg_content.getText()+"");
//            params.put("title", editText_msg_title.getText()+"");
//            params.put("emp_id", "2329");
//            params.put("type",msg_type+"");
//            params.put("name","张山");
//            params.put("timestamp",timestamp);
//            params.put("public_key", com.puman.beautyfarmcounselor.util.config.Config.public_key);
//            params.put("sign", sign);
//
//            XTHttp fh = new XTHttp();
//            fh.get(url + "", params, new AjaxCallBack<String>() {
//                @Override
//                public void onLoading(long count, long current) {
//                    editText_msg_title.setText(current + "/" + count);
//                }
//
//                @Override
//                public void onSuccess(String t) {
//                    try {
//                        JSONObject jsonObject=new JSONObject(t);
//                        Toast.makeText(getActivity(), jsonObject.get("msg").toString(), Toast.LENGTH_LONG).show();
//                        if(jsonObject.getBoolean("flag")){
//                            getActivity().getSupportFragmentManager().popBackStack();
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//
//                }
//
//                @Override
//                public void onFailure(Throwable t, int errorNo, String strMsg) {
//                    super.onFailure(t, errorNo, strMsg);
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    public void cancle() {
        init();
    }

    public void init() {
        editText_msg_content.setText("");
        editText_msg_title.setText("");
        editText_msg_title.setFocusable(true);

        msg_type = 1;
        msg_linearLayout.setBackgroundResource(R.drawable.feedback_1);
        msg_type1.setTextColor(Color.WHITE);
        msg_type2.setTextColor(Color.parseColor("#949393"));
        msg_type3.setTextColor(Color.parseColor("#949393"));

    }

    @Click({R.id.back_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstace().getSupportFragmentManager().popBackStack();
                break;
        }
    }

}
